// components/my-map/my-map.js
//高德地图的sdk
var amapFile = require("../SDK/amap-wx.130.js");
Component({

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        markers: [],
        latitude: '',
        longitude: '',
        textData: {},
        searchValue: '',
        tips:[],
        tipsShow : false,
        markerSelected:""
    },

    /**
     * 组件的方法列表
     */
    methods: {
        showMarkerInfo: function(data,i){
            var that = this;
            that.setData({
              textData: {
                name: data[i].name,
                desc: data[i].address
              }
            });
          },
        
        makertap: function(e) {
            console.log("marker"+e.markerId+" 被点击");
            var id = e.markerId;
            var that = this;
            that.setData({
                markerSelected : that.data.markers[id]
            })
            that.showMarkerInfo(this.data.markers,id);
            that.changeMarkerColor(this.data.markers,id);
        },
        
        changeMarkerColor: function(data,i){
            var that = this;
            var markers = [];
            for(var j = 0; j < data.length; j++){
            if(j==i){
                data[j].iconPath = "../../images/makerSelected.png"; //如：..­/..­/img/marker_checked.png
            }else{
                data[j].iconPath = "../../images/maker.png"; //如：..­/..­/img/marker.png
            }
            markers.push(data[j]);
            }
            that.setData({
            markers: markers
            });
        },

        // 搜索内容变化
        searchChange: function(event){
            var keywords = event.detail;
            console.log(keywords);
            var _this = this;
            var myAmapFun = new amapFile.AMapWX({key:'e3d458b0b0d8c55fa529f4dcb5834643'});
            // 根据输入内容查找附近地点
            myAmapFun.getInputtips({
                keywords : keywords,
                success : function(data){
                    console.log(data)
                    _this.setData({
                        tipsShow : true,
                        tips:data.tips
                    })
                }
            })
        },

        //提交所选的地点
        submitMarker: function(){
            console.log(this.data.markerSelected)
        },
        //点击推荐地点
        tipTap: function(e){
            const index = e.currentTarget.dataset.index;
            const item = this.data.tips[index];
            console.log(item.location.split(",")[0]);
            this.setData({
                tipsShow: false,
                searchValue: item.name,
                latitude: item.location.split(",")[1],
                longitude: item.location.split(",")[0]
            })
            //poi跳转
            this.getPoi(item.location);
        },

        //get附近的poi并展示marker
        getPoi: function(location){
            var that = this;        //that用于获取data中的数据
            var myAmapFun = new amapFile.AMapWX({key:'e3d458b0b0d8c55fa529f4dcb5834643'});
            //获取周边信息点（兴趣点）
            myAmapFun.getPoiAround({
                iconPathSelected: '../../images/makerSelected.png', //如：..­/..­/img/marker_checked.png
                iconPath: '../../images/maker.png', //如：..­/..­/img/marker.png
                location:location,
                success: function(data){
                console.log(data);
                var markersData = [];
                markersData = data.markers;
                that.setData({
                    markers: markersData
                });
                that.setData({
                    //维度
                    latitude: markersData[0].latitude
                });
                that.setData({
                    //经度
                    longitude: markersData[0].longitude
                });
                that.showMarkerInfo(markersData,0);
                },
                fail: function(info){
                wx.showModal({title:info.errMsg})
                }
            })
        }
        
    },

    // 小程序组件中的onLoad方法在此处
    lifetimes:{
        attached:function (e) {
        // 这里写onLoad代码!!!!!!!!!!!!!!!!!!!!
        this.getPoi();
        }
      },


    
})